<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lichlamviec extends CI_Controller {
	
	public	function __construct()
	{
		parent::__construct();
		if($this->session->userdata('user_id')){
		$this->load->helper("html");
		$this->load->model("laylich");
		$this->load->model("tailieus");
		$this->load->model("giaobans");
		$this->load->model("users");
		$this->load->model("lich");
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		}
		else{
			redirect('login');
		}
	}
	public function index($phongban=1,$day=null,$month=null,$year=null)
	{
		if($day == null && $month == null && $year == null){	
			$day = date("d");
			$month = date("m");
			$year = date("Y");
		}
		$menu['menu'] = 'lich'.$phongban;
		$data["phongban"] = $this->users->getTenLich($phongban);
		$data["thu2_chunhat"] = $this->laylich->thu2_chunhat($day,$month,$year); 	//return array('Thu hai'=>'2012-02-01', ..., 'Chu nhat'=>'2012-02-07')
		$data["users"] = $this->users->getUser($phongban);		
		$lich = $this->laylich->ngay($day,$month,$year,$phongban);
		$data["lichlamviec"] = array();
		$quyen = $this->users->getQuyen($this->session->userdata("user_id"));
		$data['checkQuyen'] = $this->kiemtraQuyen($phongban);
		/* $data['checkQuyen'] = FALSE;
		if($this->session->userdata("user_id") != 12 && $this->session->userdata("user_id") != 16){
			if(count($quyen) > 0){
				foreach($quyen as $q){
					if($this->users->checkQuyen($q->id_quyen,0,$phongban)){
						$data['checkQuyen'] = TRUE;
						break;
					}
				}
			}
		}
		else 
			$data['checkQuyen'] = TRUE; */
		
		//$data['checkquyen'] = $this->users->checkQuyen();	
		foreach($data["users"] as $user){
			foreach($lich as $row){						
				foreach($data["thu2_chunhat"] as $key1=>$ngay){					
					if($row["id_nguoidung"] == $user->id_nguoidung && $row["ngay_lichlamviec"] == $ngay){
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["id"] = $row["id_lichlamviec"];
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["user_id"] = $row["id_nguoidung"];
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["ngay"] = $row["ngay_lichlamviec"];
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["noidung"] = $row["noidung_lichlamviec"];
					}
				}
			}
		}
		$data["excel"] = $phongban."/".$day."/".$month."/".$year;
		$menu['list_tailieu'] = $this->tailieus->listTailieu();
		$menu['list_giaoban'] = $this->giaobans->listGiaoban();
		$menu['list_lich'] = $this->lich->listLich();
		/////////////
		$data['menu_tin'] = $this->giaobans->listGiaoban();
		$list = array();
		foreach ($data['menu_tin'] as $l){		//sap xep nhom danh sach theo giao ban cha
			if($l->id_type_giaoban != 9)
				$list[$l->cha_giaoban][] = $l;
		}		
		$menu['multi_menu_tin'] = $this->giaobans->multiGiaoban($list,0);						
		////////////
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('content_table_block_v1',$data);
		$this->load->view('footer_block');
	}
	public function kiemtraQuyen($phongban){
		//$data['typeAllow'] = ($this->giaobans->active($type,12) || $this->giaobans->active($type,13));
	
		if($this->users->checkQuyen(null,1,$phongban)){			//neu loai tin tuc, thong bao nam trong ds gioi han quyen
			$quyen = $this->users->getQuyen($this->session->userdata("user_id"));
			$checkQuyen = FALSE;
			if($this->session->userdata("user_id") != 12 && $this->session->userdata("user_id") != 16){
				if(count($quyen) > 0){
					foreach($quyen as $q){
						if($this->users->checkQuyen($q->id_quyen,1,$phongban)){
							$checkQuyen = TRUE;
							break;
						}
					}
				}
			}
			else
				$checkQuyen = TRUE;
			return $checkQuyen;
		}
		else
			return TRUE;
	}
	public function exportToExcel($phongban="lanhdao",$day=null,$month=null,$year=null)
	{
		$data["excel"] = $phongban."/".$day."/".$month."/".$year;
		$file=$data["excel"];
		header('Content-Type: text/html');
		//$table = "<table><tr><td>ddd</td></tr></table>";//i get this from another php file.It is HTML table
		header("Content-type: application/x-msexcel"); //tried adding  charset='utf-8' into header
		header("Content-Disposition: attachment; filename=$file");
		
		if($day == null && $month == null && $year == null){
			$day = date("d");
			$month = date("m");
			$year = date("Y");
		}
		$menu['menu'] = $phongban;
		$data["phongban"] = $phongban;
		$data["thu2_chunhat"] = $this->laylich->thu2_chunhat($day,$month,$year); 	//return array('Thu hai'=>'2012-02-01', ..., 'Chu nhat'=>'2012-02-07')
		$data["users"] = $this->users->getUser($phongban);
		$lich = $this->laylich->ngay($day,$month,$year,$phongban);
		$data["lichlamviec"] = array();
		$quyen = $this->users->getQuyen($this->session->userdata("user_id"));
		if(count($quyen) > 0){
			foreach($quyen as $q){
				$data['quyen'][] = $q->id_quyen;
			}
		}
		else
			$data['quyen'] = array();
		
		foreach($data["users"] as $user){
			foreach($lich as $row){
				foreach($data["thu2_chunhat"] as $key1=>$ngay){
					if($row["id_nguoidung"] == $user->id_nguoidung && $row["ngay_lichlamviec"] == $ngay){
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["id"] = $row["id_lichlamviec"];
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["user_id"] = $row["id_nguoidung"];
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["ngay"] = $row["ngay_lichlamviec"];
						$data["lichlamviec"][$user->id_nguoidung][$ngay][$row["thoidiem_lichlamviec"]]["noidung"] = $row["noidung_lichlamviec"];
					}
				}
			}
		}
		$this->load->view('content_table_block_v1',$data);
	}
	
	public function them_lich()
	{
		//echo $_POST["_date"];	
		$date = $_POST["_date"];
		$user = $_POST["_userid"];
		$noidung = nl2br(trim(($_POST["_noidung"]=="")?"-":$_POST["_noidung"]));
		$noidung = str_replace("/<br[\s\/]?>/gi", "\n", $noidung);
		$thoidiem = $_POST["_thoidiem"];
		if($this->input->post("_id") == ""){			
			$id_lich = $this->lich->them_lich($date,$user,$noidung,$thoidiem);	
			if(!$id_lich)
				echo "Lỗi: không thêm lịch!";			
			else
				echo $noidung;
		}
		else {
			$id = $this->input->post("_id");
			$id_lich = $this->lich->sua_lich($id,$noidung);
			if(!$id_lich)
				echo "Lỗi: không sửa lịch!";			
			else
				echo $noidung;
		}				
	}
}